package com.hyhy.hycloud.uaa.security;

import com.hyhy.hycloud.commons.constant.HttpHeader;
import lombok.Getter;
import lombok.ToString;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录请求详情，添加自定义字段
 *
 * @author Xuegui Yuan
 * @date 2018-09-19
 */
@ToString
public class LoginRequestDetails extends WebAuthenticationDetails {

    public static final String PASSWORD_TYPE_KEY = "passwordType";
    public static final String TFA_CODE = "tfaCode";
    public static final String OPENID_KEY = "openId";
    public static final String OPENID_VENDOR_KEY = "openIdVendor";
    public static final String APP_ID = "appId";

    @Getter
    private String appId;

    @Getter
    private PasswordType passwordType;

    @Getter
    private String openId;

    @Getter
    private String tfaCode;

    public LoginRequestDetails(HttpServletRequest request) {
        super(request);
        this.appId = request.getHeader(HttpHeader.APP_ID);
        this.tfaCode = request.getParameter(TFA_CODE);
        String passwordTypeParam = request.getParameter(PASSWORD_TYPE_KEY);
        String openIdVendorParam = request.getParameter(OPENID_VENDOR_KEY);

        openId = request.getParameter(OPENID_KEY);
        passwordType = StringUtils.isEmpty(passwordTypeParam) ? PasswordType.PASSWORD : PasswordType.valueOf(passwordTypeParam);
    }

}
